<?php
require_once('TVB/pms/pdes/action/CastGetAction.class.php');
require_once('TVB/pms/pdes/action/VideoGetAction.class.php');

class P_InfoGetAction extends BaseAction 
{
	/**
	 * @isnull false
	 */
	public $id;
	/**
	 * @isnull false
	 */
	public $act;
	/**
	 * @isnull false
	 */
	public $type;
	public $unpublish;

	public $ut;
	public $from;
	public $error_code;

	public function execute(){

		if(!is_null($this->error_code))
		{
			$this->addError('Info.Edit', array($this->error_code, ucfirst($this->type).'.Edit'));
		}

		switch($this->type)
		{
		case 'address':
			$object = "p_info_address";
			$func_name = "get";
			break;
		case 'recipe':
			$object = "p_info_recipe";
			$func_name = "get";
			break;
		case 'biography':
			$object = "p_info_biography";
			$func_name = "get";
			break;
		case 'product':
			$object = "p_info_product";
			$func_name = "get";
			break;
		case 'qa':
			$object = "p_info_q_a";
			$func_name = "get";
			break;
		case 'award':
			$object = "p_info_award";
			$func_name = "get";
			break;
		case 'general_info':
			$object = "p_info_general_information";
			$func_name = "get";
			break;
		case 'pageant':
			$object = 'p_info_pageant';
			$func_name = 'get';
			break;
		case 'song':
			$object = "p_info_song";
			$func_name = "get";
			break;
		case 'cast':
			$object = "p_cast";
			$func_name = "getDetails";
			break;
		case 'chapter_marking':
			$object = 'p_chapter';
			$func_name = "getChapterList";
			break;
		}
		try
		{
			# add publish log
			$this->setData("publishLog", publish_log::getPublishLog($object, $this->id));
            $this->setData('special_operation_premium', TRUE);

			if(!empty($this->unpublish)) {Model::$dbo['r'] = CommonUtils::getDbConn(0,'preview');}

			//object detail 
			$$object = new $object();
			$object_detail = $$object->$func_name($this->id);
			if ($this->type == 'address')
			{
				$object_detail->country_name  = country::getCountryName($object_detail->country);
				$object_detail->province_name = province_state_prefecture::getProvinceName($object_detail->province_state_prefecture);
			}
			elseif ($this->type == 'biography')
			{
				$object_detail->country_name  = country::getCountryName($object_detail->origin_country);
				$object_detail->province_name = province_state_prefecture::getProvinceName($object_detail->origin_province_state_prefecture);
			} else if ($this->type == 'pageant') {
				$object_detail->country_name  = country::getCountryName($object_detail->origin_country);
				$object_detail->province_name = province_state_prefecture::getProvinceName($object_detail
												->origin_province_state_prefecture);
				$object_detail->pageant_no = empty($object_detail->pageant_no) ? '' : $object_detail->pageant_no;
				$object_detail->astrology = empty($object_detail->astrology) ? '' : $object_detail->astrology;
				$object_detail->zodiac = empty($object_detail->zodiac) ? '' : $object_detail->zodiac;
			}
			$this->setData($this->type, $object_detail);

			//json decode
			$json_fields = AC::$p_infoJson[$object];
			$json_data = array();
			foreach ($json_fields as $field)
			{
				$json_data_decode = json_decode($object_detail->$field, true);
				$json_data[$field] =(!empty($json_data_decode)) ? $json_data_decode[$field] : array();
			}
			$list_object = $this->type."_json_list";
			$this->setData($list_object, $json_data);

			if ($this->type == 'cast')
			{
				$this->get_cast_data($object_detail);
			}
			elseif ($this->type == 'chapter_marking')
			{
				$this->get_video_data();
			}
			else
			{
				$this->get_episode_programme($object_detail->episode_id);
			}

			$this->setData("id", $this->id);
			$this->setData("act", $this->act);
			$this->setData("type", $this->type);
			$this->setData("unpublish", $this->unpublish);
			$this->setData("from", $this->from);
		    $this->setData('check_used',1);

			return 'succ';
		}
		catch(Exception $e)
		{
			error_log("info {$this->type} get: {$this->id}" . var_export($e,1));

			$this->emptyData();
			return 'system-error';
		}
	}    

	/**
	 * get cast relative data
	 */
	private function get_cast_data($cast_detail)
	{
		$action = new P_CastGetAction();
		$action->id  = $this->id;
		$action->act = 'view';
		$action->execute();

		$cast_data = $action->getData();
		foreach ($cast_data as $object=>$value)
		{
			$this->setData($object, $value);
		}
	}

	private function get_video_data()
	{
		$action = new P_VideoGetAction();
        $action->id = $this->id;
		$action->act = 'view';
		$action->execute();
		$video_data = $action->getData();

		foreach ($video_data as $object=>$value) {
			$this->setData($object, $value);
		}
	}

	/**
	 * get programme detail for cast
	 */
	private function get_programme($programme_id)
	{
		$programme = new p_programme();
		$programme_detail = $programme->getDetails($programme_id);
		$programme_name = explode("\n\t",$programme_detail->mix_programme_name);
		$programme_detail->zh_programme_name = htmlentities($programme_name[0],ENT_QUOTES,'UTF-8');
		$programme_detail->en_programme_name = htmlentities($programme_name[1],ENT_QUOTES,'UTF-8');
		$this->setData("programme", $programme_detail);
	}

	/**
	 * get programme/episode detail for info
	 */
	private function get_episode_programme($episode_id)
	{
		$episode = new p_episode();
		$episode_detail = $episode->getDetails($episode_id);
		if(isset($episode_detail->mix_title)){
			$title = explode("\n\t", $episode_detail->mix_title);
			$episode_detail->zh_title = htmlentities($title[0],ENT_QUOTES,'UTF-8');
			$episode_detail->en_title = isset($title[1]) ? htmlentities($title[1],ENT_QUOTES,'UTF-8') : '';
		}else{
			$episode_detail->zh_title = '';
			$episode_detail->en_title = '';
		}
		$this->setData("episode", $episode_detail);

		$this->get_programme($episode_detail->programme_id);
	}
}
?>
